package com.example.tys.baseapplication.net_tcp.frame.NettyImpl.tcp;


import com.example.tys.baseapplication.net_tcp.frame.future.FutureManager;
import com.example.tys.baseapplication.net_tcp.frame.packet.BasePacket;

import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeoutException;

/**
 *
 * @author tys
 * @date 2019/5/1
 */

public abstract class TcpClientFuture<PacketRequest extends BasePacket, PacketResponse extends BasePacket>
        extends AbstractTcpClient<PacketRequest, PacketResponse>{
    protected FutureManager<PacketResponse> mFutureManager = new FutureManager<>();
    private static final int DEFAULT_WAIT_TIME = 8000;

    @Override
    protected PacketResponse afterSendAndWait(PacketRequest packetRequest, int timeOutMillis) throws TimeoutException,
            InterruptedException,RejectedExecutionException {
        //不支持死等的情况
        return mFutureManager.get(packetRequest.getUniqueId(), timeOutMillis <= 0 ? DEFAULT_WAIT_TIME : timeOutMillis);
    }

    @Override
    protected void afterReceive(PacketResponse packetResponse) {
        if (null != packetResponse) {
            mFutureManager.signal(packetResponse);
        }
    }
}
